package servlet;

import entity.RatifyLesson;
import service.RatifyLessonService;
import service.impl.RatifyLessonServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/ratifyLessonListServlet")
public class RatifyLessonListServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.调用RatifyLessonService完成查询
        RatifyLessonService service = new RatifyLessonServiceImpl();
        List<RatifyLesson> ratifylessons = null;

        //获取session对象
        HttpSession session = request.getSession();
        //进行session验证
        Object obj1=session.getAttribute("identity");
        String identity = String.valueOf(obj1);
        Object obj2=session.getAttribute("username");
        String username = String.valueOf(obj2);

        if(identity.equals("teacher")){
            try {
                ratifylessons = service.findByTeacher(username);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }else{
            try {
                ratifylessons = service.findAll();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        //2.将list存入request域
        request.setAttribute("ratifylessons",ratifylessons);
        for(RatifyLesson l : ratifylessons){
            System.out.println(l);
        }
        //3.转发
        if(identity.equals("teacher")) {
            request.getRequestDispatcher("/rlist.jsp").forward(request, response);
        }else{
            request.getRequestDispatcher("/arlist.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
